/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * <p>Project: javase - Ticket
 * <p>Powered by webrx On 2021-12-24 09:26:28
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Ticket2 implements Runnable {
    private AtomicInteger num;


    public Ticket2() {
    }

    public Ticket2(int num) {
        this.num = new AtomicInteger(num);
    }

    @Override
    public void run() {
        String n = Thread.currentThread().getName();
        while (true) {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (num.get() > 0) {
                System.out.printf("%s:[售出%d号票，还剩余%d张票]%n", n, num.getAndDecrement(), num.get());
            } else {
                break;
            }

        }
    }
}
